import sqlite3,os

'''
番剧数据库类
'''
class fanju():
    def __init__(self,path):
        self.initSql='CREATE TABLE IF NOT EXISTS `fanju` (`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,`name` VARCHAR(20) NOT NULL,`time` VARCHAR(10) NOT NULL,`source` VARCHAR(100) NOT NULL,`description` text ,`state` BOOLEAN NOT NULL)'
        path=path+"/config"
        if not os.path.exists(path):
            os.mkdir(path)

        self.path=path+"/data.db"
        self.check()
    

    def check(self):
        '''
        效验
        '''
        self.conn = sqlite3.connect(self.path)
        connect = self.conn.cursor()
        connect.execute(self.initSql)
        self.conn.commit()
        self.conn.close()

    def addData(self,data):
        '''
        添加数据
        '''
        self.conn = sqlite3.connect(self.path)
        connect = self.conn.cursor()
        data=connect.execute("INSERT INTO fanju (name, time, source,description,state) VALUES ('{name}', '{time}','{source}', '{description}',{state})".format( name = data[0],time=data[1],source=data[2],description=data[3],state=data[4]))
        self.conn.commit()
        self.conn.close()
        return data

    def update(self,data):
        '''
        修改数据
        '''
        pass
        self.conn = sqlite3.connect(self.path)
        connect = self.conn.cursor()
        data=connect.execute("UPDATE fanju set name='{name}', time='{time}', source='{source}',description='{description}',state={state} where id={id} ".format(id=data[0],name = data[1],time=data[2],source=data[3],description=data[4],state=data[5]))
        self.conn.commit()
        self.conn.close()
        return data

    def delete(self,id):
        '''
        删除数据
        '''
        self.conn = sqlite3.connect(self.path)
        connect = self.conn.cursor()
        data=connect.execute("delete from fanju where id={id} ".format(id=id))
        self.conn.commit()
        self.conn.close()
        return data

    def getAllData(self):
        '''
        查询所有数据
        '''
        resultdata={}
        self.conn = sqlite3.connect(self.path)
        connect = self.conn.cursor()
        data=connect.execute('SELECT id, name, time,source, description,state  from fanju')
        for one in data:
            resultdata[one[1]]=one

        self.conn.close()
        return resultdata
    

'''
番剧数据库类
'''
class application():
    def __init__(self,path):
        self.initSql='CREATE TABLE IF NOT EXISTS `application` (`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,`name` VARCHAR(20) NOT NULL,`path` VARCHAR(100) NOT NULL,`state` BOOLEAN NOT NULL)'
        path=path+"/config"
        if not os.path.exists(path):
            os.mkdir(path)

        self.path=path+"/data.db"
        self.check()
    

    def check(self):
        '''
        效验
        '''
        self.conn = sqlite3.connect(self.path)
        connect = self.conn.cursor()
        connect.execute(self.initSql)
        self.conn.commit()
        self.conn.close()

    def addData(self,data):
        '''
        添加数据
        '''
        self.conn = sqlite3.connect(self.path)
        connect = self.conn.cursor()
        data=connect.execute("INSERT INTO application (name, path,state) VALUES ('{name}', '{path}',{state})".format( name = data[0],path=data[1],state=data[2]))
        self.conn.commit()
        self.conn.close()
        return data

    def update(self,data):
        '''
        修改数据
        '''
        pass
        self.conn = sqlite3.connect(self.path)
        connect = self.conn.cursor()
        data=connect.execute("UPDATE application set name='{name}', path='{path}', state={state} where id={id} ".format(id=data[0],name = data[1],path=data[2],state=data[3]))
        self.conn.commit()
        self.conn.close()
        return data

    def delete(self,id):
        '''
        删除数据
        '''
        self.conn = sqlite3.connect(self.path)
        connect = self.conn.cursor()
        data=connect.execute("delete from application where id={id} ".format(id=id))
        self.conn.commit()
        self.conn.close()
        return data

    def getAllData(self):
        '''
        查询所有数据
        '''
        resultdata={}
        self.conn = sqlite3.connect(self.path)
        connect = self.conn.cursor()
        data=connect.execute('SELECT id, name, path,state  from application')
        for one in data:
            resultdata[one[1]]=one

        self.conn.close()
        return resultdata 
    